#include<stdio.h>
#include<string.h>

void LamRong(char s[])
{
    int i;
    for (i=0; i<256; i++)
        s[i] = '\0';
}

int IsInteger (char s[])
{
    int i, len = strlen(s);
    for (i=0; i< len; i++)
        if ( (s[i]<48||s[i]>57) && (s[i]!=' ') )
            return 0;
    return 1;
}

void InputNumber(char n1[], char n2[])
{
    do
    {
        printf(" Nhap so nguyen lon thu nhat: ");
        gets(n1);
    }
    while(!IsInteger(n1));

    do
    {
        printf(" Nhap so nguyen lon thu hai: ");
        gets(n2);
    }
    while(!IsInteger(n2));

}

void Don(char s[], int index)
{
    int i, len = strlen(s);
    for (i=index; i<len; i++)
    {
        s[i] = s[i+1];
    }
}

void ChuanHoa(char s[])
{
    int i = 0, len = strlen(s);
    while(i<len)
    {
        if (s[i] == ' ')
        {
            Don(s, i);
            len--;
        }
        else i++;
    }
    while(s[0] == 48 && s[1] != '\0')
        Don(s, 0);
}

void Cong(char n1[], char n2[], char result[])
{
    char temp[256];
    LamRong(temp);

    int i, du = 0, len1 = strlen(n1), len2 = strlen(n2);

    for (i=0; i<len1 || du == 1; i++)
    {
        temp[i] = du;

        if(i >= len1)
        {
            temp[i] += 48;
            du = 0;
        }

        else if (i >= len2)
        {
            temp[i]+= n1[len1-i-1];
            if (temp[i] == 58)
            {
                temp[i] -= 10;
                du = 1;
            }
            else
                du = 0;
        }
        else
        {
            temp[i] += n1[len1-i-1] + n2[len2-i-1] -  48;
            if(temp[i] >57)
            {
                temp[i] -= 10;
                du = 1;
            }
            else
                du = 0;
        }
    }

    int len = strlen(temp);
    for(i=0; i<len; i++)
        result[i] = temp[len-i-1];
}

void Tru(char n1[], char n2[], char result[])
{
    char temp[256];
    LamRong(temp);
    int i, du = 0, len1 = strlen(n1), len2 = strlen(n2);

    for(i=0; i<len1; i++)
    {
        temp[i] = du + 48;
        if(i<len2)
        {
            temp[i] += n1[len1-i-1]-n2[len2-i-1];
            if(temp[i] <48)
            {
                temp[i] += 10;
                du = -1;
            }
            else
                du = 0;
        }

        else if(i<len1)
        {
            temp[i] += n1[len1-i-1];
            if(temp[i] <48)
            {
                temp[i] += 10;
                du = -1;
            }
            else
                du = 0;
        }
        else
        {

        }
    }
}

int main()
{
    char n1[256], n2[256], result[256];
    LamRong(result);
    InputNumber(n1, n2);
    ChuanHoa(n1);
    ChuanHoa(n2);

    if(strlen(n1) > strlen(n2))
        Cong(n1, n2, result);
    else
        Cong(n2, n1, result);
    printf("%s\n%s\n%s\n",n1, n2, result);
    LamRong(result);
    /*if (strlen(n1) > strlen(n2))
    {
        printf("\n%s\n%s\n", n1, n2);
    }
    else if(strlen(n1) == strlen(n2))
    {
        if( n1>n2 )
            printf("\n%s\n%s\n", n1, n2);
        else
        printf("\n%s\n%s\n", n2, n1);
    }
    ele printf("\n%s\n%s\n", n2, n1);*/
}
